const { app, BrowserWindow, ipcMain, nativeTheme } = require('electron')



function createWindow() {
  //创建浏览器窗口
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
    }
  })

  //为应用加载index.html
  win.loadFile('index.html')

  ipcMain.handle('dark-mode:toggle', () => {
    if (nativeTheme.shouldUseDarkColors) {
      nativeTheme.themeSource = 'light'
    } else {
      nativeTheme.themeSource = 'dark'
    }
    return nativeTheme.shouldUseDarkColors
  })

  ipcMain.handle('dark-mode:system', () => {
    nativeTheme.themeSource = 'system'
  })


  //打开开发者工具
  win.webContents.openDevTools()


}


function showNotification() {
  const notification = {
    title: 'Basic Notification',
    body: 'Notification from the Main process'
  }
  new Notification(notification).show()
}


//初始化完成并准备好创建浏览器窗口时调用此方法
app.whenReady().then(createWindow)


//当所有窗口都被关闭后退出
app.on('window-all-closed', () => {
  //macOs上使用Cmd+Q退出
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  //mscOS上单击dock图标并且没有其他窗口打开时
  //重新创建一个窗口
  if (BrowserWindow.getAllWindows().length == 0) {
    createWindow()
  }
})
